import * as fs from 'node:fs';
import * as path from 'node:path';
import chalk from 'chalk'

type mockersPathType = string[]

const mockersPath: mockersPathType = []
const basePath = path.join(__dirname, '../', 'src/pages');

export default (async (): Promise<mockersPathType|void> => {
  if (process.env.PROJECT_APP_MOCKER === 'true') {
    await fs.promises.readdir(basePath)
    .then(async (dirs: mockersPathType) => {
      for(let dir of dirs) {
        try {
          const mockersDirPath = `${basePath}/${dir}/mockers`
          await fs.promises.readdir(mockersDirPath)
          .then((list: mockersPathType) => {
            if (list.includes('index.ts')) {
              mockersPath.push(path.resolve(mockersDirPath, `index.ts`))
            }
          })
        } catch (e) {}
      }
    })
    if (mockersPath.length) {
      for(let path of mockersPath) {
        console.log(`<i> [${chalk.gray('mocker-api')}] ${chalk.blue('MOCKER PATH DONE')} ${chalk.green(path)} `);
      }
      console.log(`<i> [${chalk.gray('mocker-api')}] ${chalk.blue('All Mockers Complete!')}`)
    }
  } else {
    console.log(`<i> [${chalk.gray('mocker-api')}] ${chalk.rgb(15, 100, 204)('mock function not turned on! if want to use, please set "PROJECT_APP_MOCKER=true" of env')}`)
  }
  return mockersPath
})()

export { mockersPath }


